<template>
	<view class="loading-box">
		<!-- <view class="submit" @click="chooseImage">点击加载</view> -->
		<image :src="bgimg" class="bg-img"></image>
		<view class="bg"></view>
		<view class="main-box">
			<view class="img">
				<image src="../../static/images/loading.gif"></image>
			</view>
			<view class="text">检测中，请稍后</view>
		</view>
		<slideCode ref="slidecode" :modelValue="slideCodeObj" @success="onSuccess"></slideCode>
		<ad-interstitial ref="adInterstitial" :adpid="adId[0]" :loadnext="false" @load="onadload" @close="onadclose"
			@error="onaderror">
		</ad-interstitial>
	</view>
</template>
<script>
	import {
		HTTP_REQUEST_URL
	} from '@/config/app.js'
	import {
		scanningDetail,
		analysisDetail,
		getGoodsDetail
	} from '@/api/api.js'
	import slideCode from '@/uni_modules/xlg-slideCode_0.0.6/xlg_slideCode/xlg_slideCode.vue'
	export default {
		components: {
			slideCode
		},
		name: 'ScansLoading',
		data() {
			return {
				bgimg: uni.getStorageSync('submitImgSrc'),
				// retImg:'https://app.jichacha.vip/upload/e8/cb25d38c784a9658a37fa39595f734.jpg',
				retImg: 'https://app.jichacha.vip/upload/04/04cced5be51e58da8969386ec38675.jpg',
				adId: [],
				submtType: null,
				slideCodeObj: {},
				gcode: ''
			}
		},

		onReady() {
			this.showAd(this.submtType)
		},

		onLoad(opition) {
			this.submtType = opition.SubmtType
		},
		methods: {
			analysisDetailFun() {
				const that = this
				console.log(this.retImg)
				analysisDetail({
					image_url: this.retImg,
					souce: true
				}).then((res) => {
					const {
						data
					} = res
					uni.setStorageSync('RetData', data)
					setTimeout(() => {
						uni.setStorageSync('url', '/pages/scans/analysis')
						uni.reLaunch({
							url: '/pages/tabbar/index?url=true'
						})

					}, 2000)
				}).catch((e) => {
					uni.$u.toast(typeof e === 'string' ? e : '请检查网络连接');
					uni.navigateBack()
				})
			},

			// 图片验证通过
			onSuccess() {
				const that = this
				// 插屏广告
				let load_adv_status = uni.getStorageSync('load_adv_status') === undefined ? true : uni.getStorageSync(
					'load_adv_status')
				console.log(load_adv_status, '[加载页广告]')
				if (load_adv_status) {
					uni.getSystemInfo({
						success: function(res) {
							if (res.platform == 'android') {
								that.adId = ['1432350720']
							} else {
								that.adId = ['1770623670']
							}
						}
					});
					this.$refs.adInterstitial.show();
				}
				getGoodsDetail({
					gcode: this.gcode
				}).then((res) => {
					const {
						data
					} = res
					let calorie_num = parseFloat(data.info.calorie_num)
					let sugar_num = parseFloat(data.info.sugar_num)
					data.stage_list.calorie.forEach((item, index) => {
						if (calorie_num > item) {
							if (index == data.stage_list.calorie.length - 1 || calorie_num <=
								data.stage_list.calorie[index + 1]) {
								console.log(item)
								data.info.calorie_index = index + 1
							}
						}
					})
					data.stage_list.sugar.forEach((item, index) => {
						if (sugar_num > item) {
							if (index == data.stage_list.sugar.length - 1 || sugar_num <= data
								.stage_list.sugar[index + 1]) {
								data.info.sugar_index = index + 1
							}
						}
					})
					uni.setStorageSync('RetData', data)
					setTimeout(() => {
						let url = "/pages/scans/codeResults"
						if (data.info) {
							if (data.item_list.length || data.info.is_scan) {
								url = "/pages/scans/ScanResult"
							} else {
								if (data.info.name) {
									url = "/pages/scans/ScanResult"
									// url = "/pages/scans/codeResult"
								} else {
									url = "/pages/scans/codeResults?message=暂无商品信息，请联系客服处理"
								}
							}
						}
						uni.setStorageSync('url', url)
						uni.reLaunch({
							url: '/pages/tabbar/index?url=true'
						})
					}, 2000)
				})
			},

			// 获取商品信息
			scanningDetailFun() {
				const that = this
				scanningDetail({
					code: this.bgimg,
					souce: true
				}).then((ress) => {
					console.log(ress, '[[[123]]]')
					console.log(this.bgimg, '[123]')
					// 是否需要弹窗图形验证码
					this.gcode = ress.data.info.code
					if (ress.data.check == 1) {
						that.slideCodeObj = {
							className: '',
							show: true,
						}
						console.log(123, '[]')
						return
					} else {
						this.onSuccess()
					}
				}).catch((e) => {
					// uni.redirectTo({
					// 	url:"/pages/scans/ScanResult"
					// })
					console.log(e, this.bgimg, '[扫码错误]')
					let result;
					if (e.statusCode == 500) {
						result = e.data
					} else {
						result = e
					}
					uni.setStorageSync('url', "/pages/scans/codeResults?message=" + result)
					uni.reLaunch({
						url: '/pages/tabbar/index?url=true'
					})
				})
			},

			chooseImage() {
				const that = this
				let token = uni.getStorageSync('LOGIN_STATUS_TOKEN')
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success: function(res) {
						uni.setStorageSync('submitImgSrc', res.tempFilePaths[0])
						that.bgimg = res.tempFilePaths[0]
						uni.uploadFile({
							url: HTTP_REQUEST_URL + '/api/newUpload',
							filePath: that.bgimg,
							name: "file",
							header: {
								'Authorization': 'Bearer ' + token
							},
							success(res1) {
								console.log(res1)
							}
						})
					}
				});
			},

			showAd(SubmtType) {
				const that = this
				if (SubmtType == 2) {
					that.scanningDetailFun()
				} else {
					let token = uni.getStorageSync('LOGIN_STATUS_TOKEN')
					uni.uploadFile({
						url: HTTP_REQUEST_URL + '/api/newUpload',
						filePath: this.bgimg,
						name: "file",
						header: {
							'Authorization': 'Bearer ' + token
						},
						success(res) {
							let data = JSON.parse(res.data);
							that.retImg = data.data.url
							uni.setStorageSync('ScanImg', that.retImg)
							that.analysisDetailFun()
						}
					})
				}
			},

			// AD相关方法开始
			onadload(e) {
				console.log('广告数据加载成功');
			},
			// 用户点击了关闭广告
			onadclose(e) {
				console.log("onadclose", e);
			},
			// 广告加载失败
			onaderror(e) {
				console.log("onaderror: ", e.detail);
			}
			// AD相关方法结束
		}

	}
</script>
<style lang="scss">
	.submit {
		width: 300rpx;
		line-height: 100rpx;
		color: white;
		background-color: #00000050;
		text-align: center;
		position: fixed;
		z-index: 9;
	}

	.loading-box {
		width: 100vw;
		height: 100vh;

		// overflow: hidden scroll;
		.bg-img {
			width: 100vw;
			height: 100vh;
			vertical-align: top;
			position: relative;
			z-index: 0;
		}

		.bg {
			width: 100vw;
			height: 100vh;
			position: fixed;
			left: 0;
			top: 0;
			background-color: #000000;
			opacity: 0.5;
			z-index: 1;
		}

		.main-box {
			width: 500rpx;
			position: fixed;
			left: calc(50% - 250rpx);
			top: calc(50% - 300rpx);
			z-index: 2;
			text-align: center;
			line-height: 50rpx;
			color: white;
			font-size: 28rpx;
			font-weight: bold;

			.img {
				width: 500rpx;
				height: 500rpx;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
					vertical-align: top;
					display: inline-block;
				}
			}
		}
	}
</style>